﻿define(function (require) {
    require('model/AppFunc');
    var AppFuncView = require('view/AppFuncView');

    function handleATag(aTagItem) {
        var $aTagItem = $(aTagItem);
        var url = $aTagItem.attr('href');
        $aTagItem.attr('href', '#').unbind();

        $aTagItem.click(function (event) {

            if (url.indexOf('target=action') == -1)
                $.postMessage('url:' + url, '*', parent);
            else {
                $.postMessage('action:' + url, '*', parent);
            }

            event.preventDefault();
            event.stopPropagation();
        });
    };

    return Backbone.View.extend({

        el: $('#app-list'),
        events: {
    },

    initialize: function () {
        _.bindAll(this, 'render', 'addOneAppFunc');
        this.model.view = this;
    },

    render: function () {
        this.model.each(this.addOneAppFunc);

        $(this.el).slideDown();


        $(this.el).find('a.ezlink').each(function (index, item) {
            handleATag(item);
        });

        return this;
    },

    addOneAppFunc: function (appFunc, index) {
        var appFuncView = new AppFuncView({ model: new AppFunc(appFunc) });
        $(this.el).append(appFuncView.render().el);
    },

    clear: function () {
        $(this.el).empty().hide();
    }
});
});